package nc.impl.gl.glvoucher;

import java.util.HashMap;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.ResultSetProcessor;
import nc.vo.pub.BusinessException;

public abstract class AbsImpl {

	protected Map<String, Object> cacheMap = new HashMap<String, Object>();
	private BaseDAO baseDAO = null;
	
	public String getColValue(String table, String retf, String pkf, String pkv) throws DAOException {
		String key = table + retf + pkf + pkv;
		if(this.cacheMap.containsKey(key)) {
			return (String) this.cacheMap.get(key);
		}else {
			
			StringBuffer sqlB = new StringBuffer();
			sqlB.append("select ").append(retf).append(" from ").append(table).append(" where dr=0 and ").append(pkf).append(" = '").append(pkv).append("'");
			
			Object obj = getBaseDAO().executeQuery(sqlB.toString(), new ColumnProcessor());
			
			this.cacheMap.put(key, obj);
			
			return (String) obj;
		}
	}
	
	public String getColValue2(String table, String retf, String pkf, String pkv, String pkf2, String pkv2) throws DAOException {
		String key = table + retf + pkf + pkv + pkf2 + pkv2;
		if(this.cacheMap.containsKey(key)) {
			return (String) this.cacheMap.get(key);
		}else {
			
			StringBuffer sqlB = new StringBuffer();
			sqlB.append("select ").append(retf).append(" from ").append(table).append(" where dr=0 and ").append(pkf).append(" = '").append(pkv).append("' and ").append(pkf2).append(" = '").append(pkv2).append("'");
			
			Object obj = getBaseDAO().executeQuery(sqlB.toString(), new ColumnProcessor());
			
			this.cacheMap.put(key, obj);
			
			return (String) obj;
		}
	}
	
	public Object littleDataQuery(String sql, ResultSetProcessor processor) throws BusinessException {
		if(this.cacheMap.containsKey(sql)) {
			return this.cacheMap.get(sql);
		}else {
			Object obj = getBaseDAO().executeQuery(sql, processor);
			this.cacheMap.put(sql, obj);
			return obj;
		}
	}
	
	public BaseDAO getBaseDAO() {
        if (baseDAO == null) {
            baseDAO = new BaseDAO();
        }
        return baseDAO;
    }
}
